/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.chaoticengine.cgll.entity.component.render;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.chaoticengine.cgll.CGLLStateBasedGame;
import org.chaoticengine.cgll.entity.component.ActiveComponent;
import org.chaoticengine.cgll.input.Command;
import org.chaoticengine.cgll.entity.component.IUserInterfaceComponent;
import org.chaoticengine.cgll.exceptions.LoadingException;
import org.chaoticengine.cgll.ui.TextWidget;
import org.chaoticengine.cgll.ui.Widget;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.state.StateBasedGame;
import org.simpleframework.xml.Root;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
@Root(name="weapon")
public class ScoreRenderComponent extends ActiveComponent
        implements IUserInterfaceComponent {
    protected TextWidget scoreWidget = null;
    protected ArrayList<Widget> widgets = new ArrayList<Widget>();
    protected HashMap<String, Command> commands = new HashMap<String, Command>();
    protected int score = 0;

    public ScoreRenderComponent() {
    }

    @Override
    public void onSetOwner() {
        if (getOwner() == null) {
            return;
        }

        try {
            if (scoreWidget == null) {
                scoreWidget = new TextWidget(7) {

                    @Override
                    public Integer getPriority() {
                        return(100);
                    }

                    @Override
                    public void update() {
                        super.update();

                        setValue("" + score);
                    }
                };
            }
        } catch (LoadingException lEx) {
            System.out.println("Error: Failed to load font for TextWidget.");
            lEx.printStackTrace();
            System.exit(-1);
        }

        if (scoreWidget != null) {
            scoreWidget.setAlignment(Widget.Alignment.RIGHT);
            widgets.add(scoreWidget);
        }
    }

    @Override
    public Map<String, Command> getCommands() {
        return(commands);
    }

    public List<Widget> getWidgets() {
        return(widgets);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        score = ((CGLLStateBasedGame) sb).getScore();
    }
}